package view;

import control.AuktionsHandler;
import control.BrugerHandler;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferStrategy;
import javax.swing.ImageIcon;
import javax.swing.Timer;
import model.Auktion;
import model.Bruger;
import utility.ImageToolbox;
import utility.PageHandler;

/**
 *
 * @author Jeppe
 */
public class MainGui extends javax.swing.JFrame implements ActionListener {

    private PageHandler ph;
    private LoginDialog loginDialog;
    private Auktionsliste auktionsliste;
    private VisAuktionPanel visAuktionPanel;
    private BrugerHandler brugerHandler;
    private AuktionsHandler auktionsHandler;
    private Bruger aktivBruger;
    private SpotLight spotlight;
    private BufferStrategy bs;

    /**
     * Creates new form MainFrame
     */
    public MainGui() {
        initComponents();
        spotlight = new SpotLight();
        Timer timer = new Timer(100, this);
        timer.start();
        brugerHandler = new BrugerHandler();
        auktionsHandler = new AuktionsHandler();
        createBufferStrategy(2);
        bs = getBufferStrategy();
        setSize(1065, 850);
        lavLayout();
        opdaterAktivBruger();
        
        setIconImage(new ImageIcon(getClass().getResource("/images/hammer/hammer0000.png")).getImage());
    }

    private void lavLayout() {
        auktionsliste = new Auktionsliste(this);
        loginDialog = new LoginDialog(this, true);
        visAuktionPanel = new VisAuktionPanel(this);
        ph = new PageHandler((CardLayout) jPanel1.getLayout(), jPanel1);
        ph.addPage(auktionsliste, "Auktionsliste");
        ph.addPage(visAuktionPanel, "VisAuktionPanel");
        //ph.show("Auktionsliste");
    }

    public void showLoginDialog() {
        loginDialog.setVisible(true);
    }

    public void showAuktionsliste() {
        ph.show("Auktionsliste");
        System.out.println("x: "+ getSize().getWidth()+", y: " + getSize().getHeight());
    }

    public void showVisAuktionPanel(Auktion auktion) {
        visAuktionPanel.opdaterPanel(auktion);
        auktion.addListener(this);
        if (visAuktionPanel.getAuktion() != null) {
            ph.show("VisAuktionPanel");
        } else {
            System.out.println("Der er ikke angivet nogen auktion");
        }
    }

    public BrugerHandler getBrugerHandler() {
        return brugerHandler;
    }

    public AuktionsHandler getAuktionsHandler() {
        return auktionsHandler;
    }

    public void setAktivbruger(Bruger bruger) {
        aktivBruger = bruger;
        opdaterAktivBruger();
    }

    public Bruger getAktivbruger() {
        return aktivBruger;
    }

    public void opdaterAktivBruger() {

        if (getAktivbruger() == null) {
            aktivBrugerlabel.setText("Ingen");
            aktivBrugerlabel.setForeground(Color.RED);
        } else {
            aktivBrugerlabel.setText(getAktivbruger().getNavn());
            aktivBrugerlabel.setForeground(Color.black);
        }
    }

    public void paint(Graphics g) {
        
        Graphics2D g2 = (Graphics2D) bs.getDrawGraphics();
        super.paint(g2);
        spotlight.draw(g2);
        g2.dispose();
        bs.show();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        aktivBrugerlabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel_billede = new javax.swing.JPanel();
        jLabel_billede = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2_logo = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Næstved Auktionshus");
        setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Aktiv bruger:");

        jLabel_billede.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/baggrund.jpg"))); // NOI18N

        javax.swing.GroupLayout jPanel_billedeLayout = new javax.swing.GroupLayout(jPanel_billede);
        jPanel_billede.setLayout(jPanel_billedeLayout);
        jPanel_billedeLayout.setHorizontalGroup(
            jPanel_billedeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel_billede, javax.swing.GroupLayout.DEFAULT_SIZE, 1063, Short.MAX_VALUE)
        );
        jPanel_billedeLayout.setVerticalGroup(
            jPanel_billedeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_billedeLayout.createSequentialGroup()
                .addComponent(jLabel_billede, javax.swing.GroupLayout.PREFERRED_SIZE, 657, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.CardLayout());

        jPanel2.setBackground(new java.awt.Color(51, 51, 51));

        jLabel2_logo.setBackground(new java.awt.Color(51, 51, 51));
        jLabel2_logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/auktionshus_næstved_logo.png"))); // NOI18N
        jLabel2_logo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel2_logoMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel2_logoMouseExited(evt);
            }
        });
        jLabel2_logo.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                jLabel2_logoMouseMoved(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2_logo, javax.swing.GroupLayout.DEFAULT_SIZE, 1029, Short.MAX_VALUE)
                .addGap(24, 24, 24))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(jLabel2_logo, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                .addContainerGap())
        );

        jMenuBar1.setOpaque(false);

        jMenu1.setText("File");

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem3.setText("Afslut");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        jMenu3.setText("Bruger");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Login/skift bruger");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Logud");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem2);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(aktivBrugerlabel, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel_billede, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(aktivBrugerlabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGap(0, 25, Short.MAX_VALUE)
                    .addComponent(jPanel_billede, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        showLoginDialog();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        aktivBruger = null;
        opdaterAktivBruger();
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jLabel2_logoMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel2_logoMouseMoved
        spotlight.setPosition(evt.getX(), evt.getY());
    }//GEN-LAST:event_jLabel2_logoMouseMoved

    private void jLabel2_logoMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel2_logoMouseEntered
       spotlight.setEnabled(true);
    }//GEN-LAST:event_jLabel2_logoMouseEntered

    private void jLabel2_logoMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel2_logoMouseExited
        spotlight.setEnabled(false);
    }//GEN-LAST:event_jLabel2_logoMouseExited

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel aktivBrugerlabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2_logo;
    private javax.swing.JLabel jLabel_billede;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel_billede;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand() != null) {
            if (e.getActionCommand().equals("bud")) {
                auktionsliste.opdater();
                auktionsliste.revalidate();
                visAuktionPanel.opdater();
                visAuktionPanel.revalidate();
                visAuktionPanel.repaint();

                jPanel1.revalidate();
                jPanel1.repaint();
            }
        }

        // update logoanimation
        repaint();


    }
}